<template>
  <div class="gk-layout-container">
    <div class="gk-layout-header">
          <s-query perms="corewh:corewharea"   @click="findPage(null)" add=true @addClick="handleAdd" >
                <el-form-item>
                  <el-input v-model="filters.areaName" :placeholder="'请输入区域名称'" clearable></el-input>
                </el-form-item>
          
                <el-form-item>
                  <el-input v-model="filters.orderNo" placeholder="仅可输入1,2,3进行查询" auto-complete="off" clearable
                    onkeyup="value=value.replace(/[^1-3]/g,'')">
                  </el-input>
                </el-form-item>
          </s-query>
         <s-reflash reflash="true"   @findPage="findPage" > </s-reflash>
    </div>




    <div class="gk-layout-main">
      <my-table :data="pageResult.content" :loading="loading"  @selection-change="selectionChange">
          <el-table-column :label="$t('action.desc')" width="50"   header-align="center" align="center">
            <template slot-scope="scope">   {{scope.$index+1}}   </template>
          </el-table-column> 
          <el-table-column type="selection" width="40" ></el-table-column>
          <el-table-column  header-align="center"  align="center"  prop="areaName" :label="$t('field.Core.corewharea.areaName')"   key="areaName" >  </el-table-column>
          <el-table-column  header-align="center"  align="center"   prop="info" :label="$t('field.Core.corewharea.info')"   key="info" >  </el-table-column>
          <el-table-column header-align="center" align="center" prop="priority" label="优先级" key="priority" :formatter="itemFilter"> </el-table-column>
          <el-table-column  header-align="center"  align="center"   prop="orderNo" :label="$t('field.Core.corewharea.orderNO')"     key="orderNo" >  </el-table-column>
          <el-table-column  header-align="center"  align="center"    :label="$t('action.operation')" >
              <template slot-scope="scope">
                <kt-delete-button    perms="corewh:corewharea:delete"     @click="handleDelete(scope.$index, scope.row)" />
                <kt-button   :label="$t('action.edit')"  perms="corewh:corewharea:edit"    @click="handleEdit(scope.$index, scope.row)" />
              </template>
          </el-table-column>
      </my-table>
  </div>
  <!--分页栏-->
  <div class="toolbar gk-layout-footer"  style="padding:10px;">
    <kt-delete-button  perms="corewh:corewharea:delete"  
    @click="handleBatchDelete()" disable style="float:left;" :disabled="this.selections.length==0" />
    <el-pagination layout="total, prev, pager, next, jumper"  @current-change="findPage"
                   :current-page="filters.pageNum"
                   :page-size="filters.pageSize"
                   :total="pageResult.totalSize"
                   style="float:right;">
    </el-pagination>
  </div> 
    <el-dialog :title="operation?'库区 —— 新增':'库区 —— 编辑'" width="40%" :visible.sync="dialogVisible" :close-on-click-modal="false">
      <el-form :model="dataForm" label-width="90px" :rules="dataFormRules" ref="dataForm" :size="global.gSize"
        label-position="right">
        <el-form-item label="ID" prop="id" v-if="false">
          <el-input v-model="dataForm.id" :disabled="true" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item :label="$t('field.Core.corewharea.areaName')" prop="areaName">
          <el-input v-model="dataForm.areaName" auto-complete="off" clearable></el-input>
        </el-form-item>
     
        <el-form-item :label="$t('field.Core.corewharea.info')" prop="info">
          <el-input v-model="dataForm.info" auto-complete="off" clearable></el-input>
        </el-form-item>
        <s-dict-form name="classifyAreaPriority" v-model="dataForm.priority"> </s-dict-form>
        <el-form-item :label="$t('field.Core.corewharea.orderNO')" prop="orderNo">
          <el-input v-model="dataForm.orderNo" auto-complete="off" placeholder="排序"
            onkeyup="value=value.replace(/[^0-9]/g,'')" clearable></el-input>
        </el-form-item>
      
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :size="global.gSize" @click.native="dialogVisible = false">{{$t('action.cancel')}}</el-button>
        <el-button :size="global.gSize" type="primary" @click.native="submitForm" :loading="editLoading">{{$t('action.submit')}}
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import PopupTreeInput from "@/components/PopupTreeInput"
	import MyTable from "@/views/Core/MyTable"
  import KtButton from "@/views/Core/KtButton"
  import KtDeleteButton from "@/views/Core/KtDeleteButton"
   import SQuery from "@/components/Kj/SQuery" 
	import SReflash from "@/components/Kj/SReflash"
  import { format } from "@/utils/datetime"
  import { dictsFormat } from "@/utils/myUtil"
  import SDictForm from "@/components/Kj/SDictForm"
  export default {
    components: {
      PopupTreeInput,
      KtButton,
      KtDeleteButton,
      SQuery,
      SReflash,
      MyTable,
      SDictForm
    },
    data() {
      return {

        filters: {
          areaName: '', pageNum: 1, pageSize:this.global.pageSize
        },
       
        loading:false, 
        selections :[],
      
        pageResult: {},

        operation: false, // true:新增, false:编辑
        dialogVisible: false, // 新增编辑界面是否显示
        editLoading: false,

        // 新增编辑界面数据
        dataForm: {
          id: 0,
          whId: '',
          areaName: '',
          craneId: '',
          info: '',
          orderNO: '',
          locWidth: '',
          locHight: '',
          locdeep: '',
          name: 'abcdef'
        }
        

      }
    },
    computed: {
      dataFormRules() {
        const dataFormRules = {
          name: [{ required: true, message: this.getKey("user.userInput"), trigger: 'blur' }]
        };
        return dataFormRules;
      }
    },
   
    methods: {
      // 获取分页数据
      findPage: function (data) {
          if(data!=-1){
				this.filters.pageNum=data	
			}
    
        this.$api.corewharea.findPage(this.filters).then((res) => {
          this.pageResult = res.data
        })
      },

      selectionChange: function (selection) {
          this.selections = selection;
      },
      handleBatchDelete: function () {
        let ids = this.selections.map(item => item.id).toString()
        let params = []
          let idArray = (ids + '').split(',')
          for (var i = 0; i < idArray.length; i++) {
            params.push({ 'id': idArray[i] })
          }
          this.$api.corewharea.batchDelete(params).then((res) => {
              if(res.code=="500"){ 
                this.$message({ message: this.getKey('action.operateFail') + res.msg, type: 'error' })
              }
              this.findPage(-1)
          });
      },
      // 批量删除
      handleDelete: function (index ,data) {
         let params = [{ 'id': data.id }]
         this.$api.corewharea.batchDelete(params).then((res) => {
            if(res.code=="500"){
                this.$message({ message: this.getKey('action.operateFail') + res.msg, type: 'error' })
            }
            this.findPage(-1)
        });
  
      }, 
      handleAdd: function () {
        this.dialogVisible = true
        this.operation = true
        this.dataForm = {
          id: 0,
          whId: '',
          areaName: '',
          craneId: '',
          info: '',
          orderNO: '',
          locWidth: '',
          locHight: '',
          locdeep: ''
        }
      },
      itemFilter: function (row, column, val, index) {
        return dictsFormat("classifyAreaPriority", val)
      },
      // 显示编辑界面
      handleEdit: function (params,row) {
        this.dialogVisible = true
        this.operation = false
        this.dataForm = Object.assign({}, row)
      },
      // 编辑
      submitForm: function () {
        this.$refs.dataForm.validate((valid) => {
          if (valid) {
            this.$confirm(this.getKey('action.isConfirm'), this.getKey('action.tips'), {}).then(() => {
                this.editLoading = true
                let params = Object.assign({}, this.dataForm)
                this.$api.corewharea.save(params).then((res) => {
                    this.editLoading = false
                    if (res.code == 200) {
                      this.$message({ message: this.getKey('action.operateSucess'), type: 'success' })
                      this.dialogVisible = false
                      this.$refs['dataForm'].resetFields()
                    } else {
                      this.$message({ message: this.getKey('action.operateFail') + res.msg, type: 'error' })
                    }
                    this.findPage(-1)
                })
            })
          }
        })
      },
      // 获取部门列表
      findDeptTree: function () {
        this.$api.dept.findDeptTree().then((res) => {
          this.deptData = res.data
        })
      },
      // 菜单树选中
      deptTreeCurrentChangeHandle(data, node) {
        this.dataForm.deptId = data.id
        this.dataForm.deptName = data.name
      },
      // 时间格式化
      dateFormat: function (row, column, cellValue, index) {
        return format(row[column.property])
      },
     
    
      
      getKey: function (arg) {
        return this.$t(arg)
      },
    

    },
    created() {
      this. findPage(1);
    }
  }
</script>

<style scoped>
</style>